//
//  CZToolbar.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/29.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZToolbar: UIView {
    
    var status: CZStatus? {
        didSet {
            retweetButton?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            commentButton?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            unlikeButton?.setTitle(status?.attitudesStr, forState: UIControlState.Normal)
        }
    }
        /// 转发按钮
    var retweetButton: UIButton?
        /// 评论按钮
    var commentButton: UIButton?
        /// 点赞按钮
    var unlikeButton: UIButton?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK - 设置视图
    private func setupUI() {
        //添加子视图
        retweetButton = addChildButtons("timeline_icon_retweet", title: "转发")
        commentButton = addChildButtons("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButtons("timeline_icon_unlike", title: "赞")
        let lineImageView1 = addLineImageView()
        let lineImageView2 = addLineImageView()
        
        //添加约束
        retweetButton?.snp_makeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton?.snp_makeConstraints { (make) in
            make.left.equalTo(retweetButton!.snp_right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton?.snp_makeConstraints { (make) in
            make.left.equalTo(commentButton!.snp_right)
            make.right.top.bottom.equalTo(self)
        }
        lineImageView1.snp_makeConstraints { (make) in
            make.centerX.equalTo(retweetButton!.snp_right)
            make.centerY.equalTo(self)
        }
        lineImageView2.snp_makeConstraints { (make) in
            make.centerX.equalTo(commentButton!.snp_right)
            make.centerY.equalTo(self)
        }
    }
    
    private func addLineImageView()-> UIImageView {
        let lineImageView = UIImageView(imagName: "timeline_card_bottom_line")
        addSubview(lineImageView)
        return lineImageView
    }
    
    private func addChildButtons(imagName: String,title: String) -> UIButton {
        
            let btn = UIButton()
            btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
            btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
            
            btn.setImage(UIImage(named: imagName), forState: UIControlState.Normal)
        
            btn.setTitle(title, forState: UIControlState.Normal)
            btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
            addSubview(btn)
            return btn
    }
}
